home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1998 April
/
MACPOWER-1998-04.ISO.7z
/
MACPOWER-1998-04.ISO
/
Shareware Paradise
/
Tabler.091.sit
/
Tabler 0.9.1
/
source code
/
Convert.c
next >
Wrap
Text File
|
1996-11-02
|
6KB
|
223 lines
/*
*--------------------------------------------------------------
* Convert.c
*--------------------------------------------------------------
* the heart of Tabler
*--------------------------------------------------------------
*/
/* System Include Headers */
#include <Types.h>
#include <Memory.h>
#include <TextUtils.h>
#include <Resources.h>
#include <Files.h>
#include <Folders.h>
#include <Sound.h>
#include <Errors.h>
/* Project Include Header */
#include "Tabler.h"
/* Resource IDs */
enum constSoudID {
kMySoundID = 8192
};
/* Global data for HTML tags */
static unsigned char gTab[] = {"¥p¥t"};
static unsigned char gComma1[] = {"¥p,"};
static unsigned char gComma2[] = {"¥p, "};
static unsigned char gReturn[] = {"¥p¥r"};
static unsigned char gTagMark[] = {"¥p<table "};
static char tagTableBgn[] = {"<table border cellpadding cellspacing width>¥r<tr><td>"};
static char tagTableEnd[] = {"</table>¥r"};
static char tagTableClm[] = {"</td><td>"};
static char tagTableRow[] = {"</td></tr>¥r<tr><td>"};
static char tagTableOne[] = {"<tr><td>"};
/* Static function prototypes */
static OSErr MakeTableTags(Handle src, Handle dst);
static short GetALine(Ptr *buff);
static OSErr ConvertTags(Handle src, Handle dst);
static OSErr Replace(Handle target, Ptr subst, short len, StringPtr key);
static Boolean IsTableTag(Handle);
/* Implementation */
/*
*--------------------------------------------------------------
* ConvertScrap
*--------------------------------------------------------------
* get a text from scrap, if any, and make it to be converted
*--------------------------------------------------------------
*/
OSErr ConvertScrap(void)
{
OSErr result = noErr;
long scrapOffset;
long scrapCount = GetScrap(nil, 'TEXT', &scrapOffset);
if (scrapCount > 0) {
/* do convert if TEXT exits */
Handle srcHandle = NewHandle(0);
Handle dstHandle = nil;
if (srcHandle != nil) {
/* really get the scrap data */
GetScrap(srcHandle, 'TEXT', &scrapOffset);
if (IsTableTag(srcHandle) == false) {
dstHandle = NewHandle(0);
if (dstHandle != nil) {
/* then convert it */
SoundMyClick();
result = MakeTableTags(srcHandle, dstHandle);
if (result == noErr) {
/* put it into the scrap */
ZeroScrap();
HLock(dstHandle);
result = PutScrap(GetHandleSize(dstHandle), 'TEXT', *dstHandle);
HUnlock(dstHandle);
}
} else {
result = MemError();
}
}
} else {
result = MemError();
}
/* clean up */
if (srcHandle != nil) DisposeHandle(srcHandle);
if (dstHandle != nil) DisposeHandle(dstHandle);
}
return result;
}
/*
*--------------------------------------------------------------
* MakeTableTags
*--------------------------------------------------------------
* convert from a given source data to a destination data
*--------------------------------------------------------------
*/
static OSErr MakeTableTags(Handle srcData, Handle dstData)
{
OSErr result = nilHandleErr;
if ((srcData != nil) && (dstData != nil)) {
Size dataLen;
Handle tmpData = NewHandle(0);
if (tmpData != nil) {
result = HandAndHand(srcData, tmpData);
}
if (result == noErr) {
dataLen = (GetHandleSize(tmpData));
if ((*tmpData)[dataLen-1] != '¥r') {
SetHandleSize(tmpData, dataLen+1);
result = ResError();
}
}
if (result == noErr) {
(*tmpData)[dataLen] = '¥r';
result = Replace(tmpData, tagTableRow, sizeof(tagTableRow)-1, gReturn);
}
if (result >= noErr) {
/* try tab at first */
result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gTab);
if (result == noErr) {
/* then try "comma + space" */
result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gComma2);
if (result == noErr) {
/* try comma at last */
result = Replace(tmpData, tagTableClm, sizeof(tagTableClm)-1, gComma1);
}
}
}
if (result >= noErr) {
SetHandleSize(tmpData, GetHandleSize(tmpData) - (sizeof(tagTableOne)-1));
result = MemError();
}
if (result == noErr) {
result = PtrAndHand(tagTableBgn, dstData, sizeof(tagTableBgn)-1);
}
if (result == noErr) {
result = HandAndHand(tmpData, dstData);
}
if (result == noErr) {
result = PtrAndHand(tagTableEnd, dstData, sizeof(tagTableEnd)-1);
}
if (tmpData != nil) {
DisposeHandle(tmpData);
}
}
return result;
}
/*
*--------------------------------------------------------------
* Replace
*--------------------------------------------------------------
* replace the given words using ReplaceText
*--------------------------------------------------------------
*/
static OSErr Replace(Handle targetText, Ptr substPtr, short length, StringPtr keyWord)
{
Handle substText;
short result;
result = PtrToHand(substPtr, &substText, length);
if (result == noErr) {
result = ReplaceText(targetText, substText, keyWord);
DisposeHandle(substText);
}
return (OSErr)result;
}
/*
*--------------------------------------------------------------
* IsTableTag
*--------------------------------------------------------------
* check whether if the text is already HTML table tag
*--------------------------------------------------------------
*/
static Boolean IsTableTag(Handle theText)
{
Str31 testStr;
Size testSize = Length(gTagMark);
UInt8 hState = HGetState(theText);
/* make a string from the text handle */
testStr[0] = testSize;
HLock(theText);
BlockMoveData(*theText, &testStr[1], testSize);
HSetState(theText, hState);
return (EqualString(testStr, gTagMark, false, true));
}
/*
*--------------------------------------------------------------
* PlayAClick
*--------------------------------------------------------------
* play a custom click sound
*--------------------------------------------------------------
*/
void PlayAClick(void)
{
Handle aSound;
aSound = GetResource('snd ', kMySoundID);
if (aSound != nil) {
DetachResource(aSound);
SndPlay(nil, (SndListHandle)aSound, true);
}
/*
The reason we have to detach the sound resource is that we have
to close the current resource fork immediately in case of FKEY.
As of Sound Manager 3.1, SndPlay play sound asyncronously
even if we set the flag to true. It will be changed, however,
in the future version of Sound Manager. Hence we don't dispose
the sound handle here. Don't forget to set the sound resource
unlocked and PURGEABLE.
*/
}
/* end of Convert.c */